import cherrypy
import os, sys

class TvEdit(object):

    def __init__(self):
        file = open("channellist", "r")
        self.tvList = file.readlines()
        file.close()

    def showList(self):
        resultStr = """
        <html>
        <title>TV Edit</title>

	    <body><center>
            <table>
            <tr>
                <th>Channel Name</th>
                <th>Brocaster</th>
                <th>URL</th>
		        <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
		        <td><a href="editList">New Channel</a></td>
            </tr>
        """

        for item in self.tvList:
            tmpItem = item.split()
            if(len(tmpItem) == 3):
                resultStr = resultStr + """
				<form action="editList" method="GET">
                <tr>
                    <td><input type="text" name="name" value=%s readonly="true" /></td>
                    <td><input type="text" name="publisher" value=%s readonly="true" /></td>
                    <td><input type="text" name="address" value=%s readonly="true" /></td>
					<td><input type="submit" value="Edit Channel" /></td>
                    <td><a href="delItem?name=%s&publisher=%s&address=%s">Delete Channel</a></td>
                </tr>
				</form>
                """ %(tmpItem[0], tmpItem[1], tmpItem[2], tmpItem[0], tmpItem[1], tmpItem[2])
        
        resultStr = resultStr + """
		</table>
	    </center></body>
        </html>
        """

        return resultStr
    showList.exposed = True

    def editList(self, name = None, publisher = None, address = None):
        delItem = self.searchItem(name, publisher, address)
        if(delItem):
            self.tvList.remove(delItem)

        return """
	    <html>
		<title>Edit Item</title>
	    <body><center>
		    <form action="setTvList" method="GET"><table>
    		<tr>

		<td>Channel Name:</td><td>
	<input type="text" name="name" value=%s  /></td></tr>
			<tr>
                <td>Brocaster:</td>
                <td><input type="text" name="publisher", value=%s /></td>
            </tr>
			<tr>
                <td>URL:</td>
                <td><input type="text" name="address", value=%s /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Confirm"/> <input type="reset"/></td></tr>
            </table></form>
	    </center></body>
    	</html>
        """ %(name, publisher, address)
    editList.exposed = True

    def delItem(self, name = None, publisher = None, address = None):
        delItem = self.searchItem(name, publisher, address)
        if(delItem):
            self.tvList.remove(delItem)
        self.setFile()
        return self.showList()
    delItem.exposed = True

    def setTvList(self, name = None, publisher = None, address = None):
        if(self.searchItem(name, publisher, address)):
            return self.showList()

        result = name+" "+publisher+" "+address+"\n"
        self.tvList.append(result);
        self.tvList.sort()

        self.setFile()
        return self.showList()
    setTvList.exposed = True

    def searchItem(self, name = None, publisher = None, address = None):
        for tmp in self.tvList:
            tmpItem = tmp.split()
            if((name == tmpItem[0]) and (publisher == tmpItem[1]) and (address == tmpItem[2])):
                return tmp
        else:
            return None
    searchItem.exposed = True

    def setFile(self):
        file = open("name.log", "w")
        for item in self.tvList:
            file.write(item)
        file.close()
    setFile.exposed = True

    def list(self):
	file = open("name.log", "r")
        self.tvList = file.readlines()
        file.close()
        resultStr = ""
        for item in self.tvList:
            tmpItem = item.split()
            if(len(tmpItem) == 3):
                resultStr = resultStr + "%s$%s$%s#" %(tmpItem[0], tmpItem[1], tmpItem[2])
        return resultStr
    list.exposed = True

cherrypy.quickstart(TvEdit())
